package com.xxl.job.executor.crawler.job;

import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.crawler.XxlCrawler;
import com.xxl.job.executor.crawler.annotation.PageFieldSelect;
import com.xxl.job.executor.crawler.annotation.PageSelect;
import com.xxl.job.executor.crawler.conf.XxlCrawlerConf;

import com.xxl.job.executor.crawler.entity.model.BdasCrawlerProductDetail;
import com.xxl.job.executor.crawler.entity.model.BdasCrawlerSpecialProduct;
import com.xxl.job.executor.crawler.mapper.BdasCrawlerProductDetailMapper;
import com.xxl.job.executor.crawler.mapper.BdasCrawlerSpecialProductMapper;
import com.xxl.job.executor.crawler.parser.PageParser;
import com.xxl.job.executor.crawler.util.JedisUtil;
import com.xxl.job.executor.crawler.util.UUIDUtil;

import com.xxl.job.executor.crawler.util.SkuAndItemIdUtil;
import lombok.Data;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.sql.Timestamp;
import java.util.*;

/**
 * 描述:
 * 跟卖商品
 * //给String[] link 做判断，如果redis为空执行数据库操作
 * @author Pfly
 * @create 2019-01-02 15:28
 */

@Service
public class AllSpecialProduct {
    @PageSelect(cssQuery = "body")
    @Data
    public static class PageVo {
        /*商品名称*/
        @PageFieldSelect(cssQuery = ".moreOfferFor > h3", selectType = XxlCrawlerConf.SelectType.HTML)
        private String special_name;

        /*跟卖商家*/
        @PageFieldSelect(cssQuery = "#condition-all .seller-name > span > a", selectType = XxlCrawlerConf.SelectType.HTML)
        private List<String> sepcialProductMerchant;

        /*商品sku和itemid*/
        @PageFieldSelect(cssQuery = "#condition-all div.row div.seller-field div.field", selectType = XxlCrawlerConf.SelectType.HTML)
        private List<String> special_skuAndItemId;
    }

    @Autowired
    private BdasCrawlerSpecialProductMapper bdasCrawlerSpecialProductMapper;

    @Autowired
    private BdasCrawlerProductDetailMapper bdasCrawlerProductDetailMapper;

    @Test
    public void start_crawler_sepcial() {

        // 拿到数据的时间
        final Timestamp begind_time = new Timestamp(System.currentTimeMillis());

        // 用以存放结果对象的集合
        final List<BdasCrawlerSpecialProduct> specialProductEntityList = new ArrayList<>();


        Map headerMap = new HashMap();
        Map cookieMap = new HashMap();
        Map paramMap = new HashMap();

        cookieMap.put("BUYER_CITY_SELECTED", "80");

        headerMap.put("Cache-Control", "max-age=0");

        paramMap.put("type","3");

        // 判断如果redis里面有数据执行redis
        if (JedisUtil.selectSepcialUrlByRedis().size() != 0) {

            List list = JedisUtil.selectSepcialUrlByRedis();

            // 给url数组赋长度：集合长度
            final String[] link = new String[list.size()];
            Jedis jedis = JedisUtil.getJedis();
            // 循环遍历集合中的url添加到数组中
            for (int i = 0; i < list.size(); i++) {
                link[i] = jedis.lpop("sepcialUrl");
            }

            XxlJobLogger.log("初始化任务数量:" + list.size());

            crawlerUrl(link, cookieMap, headerMap,paramMap,
                    bdasCrawlerSpecialProductMapper, bdasCrawlerProductDetailMapper, specialProductEntityList, begind_time);
        } else{

            // 如果redis中没数据，则执行数据库
            List<BdasCrawlerProductDetail> bdasCrawlerProductDetailDaoAll = bdasCrawlerProductDetailMapper.findAllByStatus();

            // 给url数组赋长度：集合长度
            final String[] link = new String[bdasCrawlerProductDetailDaoAll.size()];

            // 循环遍历集合中的url添加到数组中
            for (int i = 0; i < link.length; i++) {
                link[i] = bdasCrawlerProductDetailDaoAll.get(i).getMoreOffersProductHref();
            }

            XxlJobLogger.log("初始化任务数量:" + bdasCrawlerProductDetailDaoAll.size());
            // 爬虫代码
            crawlerUrl(link, cookieMap, headerMap,paramMap,
                    bdasCrawlerSpecialProductMapper, bdasCrawlerProductDetailMapper, specialProductEntityList, begind_time);
        }
    }
    /**
     *  封装爬虫
     */
    public static void crawlerUrl(String[] link, Map cookieMap, Map headerMap,Map paramMap,
                                  BdasCrawlerSpecialProductMapper bdasCrawlerSpecialProductMapper,
                                  BdasCrawlerProductDetailMapper bdasCrawlerProductDetailMapper,
                                  final List<BdasCrawlerSpecialProduct> specialProductEntityList,
                                  final Timestamp begind_time){
        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls(link)
                .setThreadCount(3).setPauseMillis(1000)
                .setAllowSpread(false)
                //.setProxyMaker(proxyMaker)
                .setCookieMap(cookieMap)
                .setHeaderMap(headerMap)
                .setParamMap(paramMap)
                .setPageParser(
                        new PageParser<PageVo>() {
                            @Override
                            public boolean parse(Document html, Element pageVoElement, PageVo pageVo) {

                                List<BdasCrawlerSpecialProduct> list = new ArrayList<>();

                                List<String> sku = SkuAndItemIdUtil.getSku(pageVo.getSpecial_skuAndItemId());

                                List<String> itemId = SkuAndItemIdUtil.getItemId(pageVo.getSpecial_skuAndItemId());


                                System.out.println(pageVo.getSepcialProductMerchant().size());

                                for (int i = 0; i < itemId.size(); i++) {
                                    BdasCrawlerSpecialProduct entity = new BdasCrawlerSpecialProduct();

                                    entity.setSepcialProductMerchant(pageVo.getSepcialProductMerchant().get(i));

                                    entity.setSpecialProductSku(sku.get(i));

                                    entity.setSpecialProductItemId(itemId.get(i));
                                    list.add(entity);
                                }

                                for (BdasCrawlerSpecialProduct tem : list) {
                                    tem.setGenerateDate(begind_time);
                                    tem.setSpecialProductId(UUIDUtil.getId());
                                    tem.setSpecialProductName(pageVo.getSpecial_name());
                                    //将遍历的对象循环插入list中
                                    specialProductEntityList.add(tem);
                                }

                                return true;

                            }
                        }).build();
        XxlJobLogger.log("任务构建完毕，开始执行！");
        crawler.start(true);
        XxlJobLogger.log("执行完毕！");
        XxlJobLogger.log("执行成功数量数量:" + specialProductEntityList.size());

        //int i  = 1;
        // 开启持久化,编辑装有对象的集合，把插入成功的数据放入数据库
        /*for (BdasCrawlerSpecialProduct bdasCrawlerSpecialProductEntity : specialProductEntityList) {
            bdasCrawlerSpecialProductMapper.insertSelective(bdasCrawlerSpecialProductEntity);
           // XxlJobLogger.log("正在持久化"+ i++ +"条");
            i++;
        }*/
        int insert = bdasCrawlerSpecialProductMapper.insert(specialProductEntityList);

        XxlJobLogger.log("持久化成功"+ insert +"条");

        // 当对象不能null的时候，给详情表里状态改成1

        bdasCrawlerProductDetailMapper.updateStatusByType();


        XxlJobLogger.log("持久化结束！");
        if (!crawler.isRunState()) {
            XxlJobLogger.log("-----------------这个类别已结束-------------------------");
        }
    }
}
